package com.tz.tzgw.pc.web.dao;

import com.tz.tzgw.pc.web.model.Article;
import com.tz.tzgw.pc.web.model.PageResult;
import com.tz.tzgw.pc.web.model.ZtImages;
import com.tz.tzgw.pc.web.model.ZtVideo;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class ZtVideoDao {
    private final JdbcTemplate jdbcTemplate;

    public ZtVideoDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    public PageResult<ZtVideo> getZtVideosByPageAndCondition(int pageNum, int pageSize, Long setid, Integer istop,Integer cateid) {
        PageResult<ZtVideo> pageResult = new PageResult<>();
        int offset = (pageNum - 1) * pageSize;

        // 构建查询总条数的 SQL
        StringBuilder countSqlBuilder = new StringBuilder("SELECT COUNT(*) FROM tz_zt_video");
        List<Object> countParams = new ArrayList<>();

        // 构建分页查询的 SQL
        StringBuilder querySqlBuilder = new StringBuilder("SELECT * FROM tz_zt_video");
        List<Object> queryParams = new ArrayList<>();


        String wherestr = " WHERE";

        if (setid != null){
            countSqlBuilder.append(wherestr+" setid = ?");
            querySqlBuilder.append(wherestr+" setid = ?");
            countParams.add(setid);
            queryParams.add(setid);
        }

        if (istop != null){
            if (setid != null){
                wherestr = " AND";
            }

            countSqlBuilder.append(wherestr+" istop = ?");
            querySqlBuilder.append(wherestr+" istop = ?");
            countParams.add(istop);
            queryParams.add(istop);
        }

        if (cateid != null){
            if (setid != null || istop != null){
                wherestr = " AND";
            }

            countSqlBuilder.append(wherestr+" cateid = ?");
            querySqlBuilder.append(wherestr+" cateid = ?");
            countParams.add(cateid);
            queryParams.add(cateid);
        }



        // 查询总条数
        String countSql = countSqlBuilder.toString();
        int total = jdbcTemplate.queryForObject(countSql, Integer.class, countParams.toArray());
        pageResult.setTotal(total);

        // 分页查询
        querySqlBuilder.append(" ORDER BY sort_order DESC LIMIT ?, ?");
        queryParams.add(offset);
        queryParams.add(pageSize);

        String querySql = querySqlBuilder.toString();
        List<ZtVideo> videos = jdbcTemplate.query(querySql, new BeanPropertyRowMapper<>(ZtVideo.class), queryParams.toArray());
        pageResult.setData(videos);

        return pageResult;
    }


    public int insertOrUpdateZtVideo(ZtVideo ztVideo) {
        Long id = ztVideo.getId();
        if (id != null) {
            // Update
            String sql = "UPDATE tz_zt_video SET ";
            List<String> updateFields = new ArrayList<>();
            List<Object> params = new ArrayList<>();

            if (ztVideo.getTitle() != null) {
                updateFields.add("title = ?");
                params.add(ztVideo.getTitle());
            }
            if (ztVideo.getSetid() != null) {
                updateFields.add("setid = ?");
                params.add(ztVideo.getSetid());
            }

            if (ztVideo.getCateid() != null) {
                updateFields.add("cateid = ?");
                params.add(ztVideo.getCateid());
            }
            if (ztVideo.getThumb() != null) {
                updateFields.add("thumb = ?");
                params.add(ztVideo.getThumb());
            }
            if (ztVideo.getVideo() != null) {
                updateFields.add("video = ?");
                params.add(ztVideo.getVideo());
            }
            if (ztVideo.getHit() != null) {
                updateFields.add("hit = ?");
                params.add(ztVideo.getHit());
            }
            if (ztVideo.getIstop() != null) {
                updateFields.add("istop = ?");
                params.add(ztVideo.getIstop());
            }
            if (ztVideo.getIstui() != null) {
                updateFields.add("istui = ?");
                params.add(ztVideo.getIstui());
            }
            if (ztVideo.getStatus() != null) {
                updateFields.add("status = ?");
                params.add(ztVideo.getStatus());
            }

            if (ztVideo.getSort_order() != null) {
                updateFields.add("sort_order = ?");
                params.add(ztVideo.getSort_order());
            }

            sql += String.join(", ", updateFields);
            sql += " WHERE id = ?";

// 添加 id 参数
            params.add(id);

            jdbcTemplate.update(sql, params.toArray());
        } else {
            // Insert
            String sql = "INSERT INTO tz_zt_video (title, cateid, setid, thumb, video, hit, istop, istui,status,sort_order) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            Object[] params = new Object[] {
                    ztVideo.getTitle(),
                    ztVideo.getCateid(),
                    ztVideo.getSetid(),
                    ztVideo.getThumb(),
                    ztVideo.getVideo(),
                    ztVideo.getHit(),
                    ztVideo.getIstop(),
                    ztVideo.getIstui(),
                    ztVideo.getStatus(),
                    ztVideo.getSort_order()
            };

            jdbcTemplate.update(sql, params);
        }
        return 1;
    }

    public ZtVideo getVideoById(Long id) {
        String sql = "SELECT * FROM tz_zt_video WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(ZtVideo.class));
    }
    public Integer deleteVideo(Integer id) {
        String sql = "DELETE FROM tz_zt_video WHERE id = ?";
        jdbcTemplate.update(sql, id);
        return 1;
    }

    public ZtVideo getVideoByTopOne(Long setid) {
        String sql = "SELECT * FROM tz_zt_video WHERE status = 1 AND istop = 2 AND setid=?  ORDER BY create_at DESC LIMIT 1";
        try {
            return jdbcTemplate.queryForObject(sql, new Object[]{setid}, new BeanPropertyRowMapper<>(ZtVideo.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    public void incrementField(Long id, String fieldName) {
        String sql = "UPDATE tz_zt_video SET " + fieldName + " = " + fieldName + " + 1 WHERE id = ?";
        jdbcTemplate.update(sql, id);
    }


}
